package org.test.client.ui.component.paging;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JToggleButton;

import org.test.client.listener.ClickAction;
import org.test.client.listener.CommonMouseListener;
import org.test.client.ui.util.Icons;
import org.test.client.ui.util.UIUtil;

public class PagingBtn extends JToggleButton{
	private PagingBtn thisBtn = this;
	private PagingBar parentBar = null;
	
	public static Color normalBackColor = Color.white;
	public static Color rollOverColor = Icons.getColor("f5f5f5");//
	public static Color borderColor = Icons.getColor("dddddd");
	public static Color fontColor = Icons.getColor("2390cf");
	public static Color selecedFontColor = Icons.getColor("999999");
	
	int thisBtnPage = 0;
	String txt = "";
	boolean selected = false;
	boolean rollOvered = false;
	CommonMouseListener listener = null;
	
	public boolean needRightBorderLine= false;
	
	public int currentw = PagingBar.btnWidth;
	public int currenth = PagingBar.btnHeight;
	
	public PagingBtn(int pageNum){
		thisBtnPage = pageNum;
		txt = pageNum+"";
		doInit();
	}
	
	public PagingBtn(String pageNum){
		thisBtnPage = Integer.parseInt(pageNum);
		txt = pageNum;
		doInit();
	}
	
	public void doInit(){
		
		listener = new CommonMouseListener(thisBtn){
			@Override
			public void mouseEntered(MouseEvent e) {
				if(!thisBtn.isSelected()){
					super.mouseEntered(e);
					setRollOvered(true);
				}
			}
			@Override
			public void mouseExited(MouseEvent e) {
				super.mouseExited(e);
				setRollOvered(false);
			}
		};
		listener.addClickAction(new ClickAction() {
			@Override
			public Object doOnClick(MouseEvent ev) {
				if(parentBar != null){
					parentBar.setCurrentPage(thisBtnPage);
				}
				return null;
			}
		});
		
		
		checkWidth();
		this.setPreferredSize(new Dimension(currentw,currenth));
		this.addMouseListener(listener);
	}
	
	@Override
	protected void paintComponent(Graphics g) {
//		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D)g;
		UIUtil.setAntiAliasing(g2d, true);
		int w = this.getWidth();
		int h = this.getHeight();
		
		//paint BackGround
		if(thisBtn.isSelected() || rollOvered){
			g2d.setColor(rollOverColor);
		}else{
			g2d.setColor(normalBackColor);
		}
		g2d.fillRect(0, 0, w, h);
		
		//drawTxt
		int x = w/2 - UIUtil.getStringWidth(txt, g2d.getFontMetrics())/2;
		int y = h/2 + UIUtil.getTxty(g2d);
		if(thisBtn.isSelected()){
			g2d.setColor(selecedFontColor);
		}else{
			g2d.setColor(fontColor);
		}
		g2d.drawString(txt, x, y);
		
	}
	
	@Override
	protected void paintBorder(Graphics g) {
//		super.paintBorder(g);
		Graphics2D g2d = (Graphics2D)g;
		UIUtil.setAntiAliasing(g2d, true);
		int w = this.getWidth();
		int h = this.getHeight();
		
		g2d.setColor(borderColor);
		//top
		g2d.drawLine(0, 0, w, 0);
		//left
		g2d.drawLine(0, 0, 0, h);
		//bottom
		g2d.drawLine(0, h-1, w, h-1);
		//right
		if(needRightBorderLine){
			g2d.drawLine(w-1, 0, w-1, h);
		}
	}
	
	public void setRollOvered(boolean flag){
		if(flag == rollOvered){
			return;
		}
		rollOvered = flag;
		thisBtn.repaint();
	}
	

	public void setPagingNum(int num) {
		thisBtnPage = num;
		this.txt = num+"";
		checkWidth();
		this.repaint();
	}

	public void setPagingNum(String num) {
		thisBtnPage = Integer.parseInt(num);
		this.txt = num;
		checkWidth();
		this.repaint();
	}

	/**
	 * 检查默认 宽度是否 能容纳 当前字符串
	 */
	public void checkWidth(){
		FontMetrics fm = this.getFontMetrics(this.getFont());
		int w = UIUtil.getStringWidth(txt, fm);
		if(w > currentw){
			currentw = w+4;
		}
		this.setPreferredSize(new Dimension(currentw,currenth));
	}
	
	public int getCurrrentPageNum() {
		return thisBtnPage;
	}


	public void setParentBar(PagingBar parentBar) {
		this.parentBar = parentBar;
	}
	
	public void setCurrentH(int h ){
		currenth = h;
		this.setPreferredSize(new Dimension(this.getPreferredSize().width,currenth));
	}
	
}
